package com.tanhua.api.impl.mongo;

import com.tanhua.api.mongo.MessageApi;
import com.tanhua.domain.mongo.Publish;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;

import java.util.List;

@Service
public class MessageApiIml implements MessageApi {

   @Autowired
   private MongoTemplate mongoTemplate;
    @Override
    public Boolean updateStatus(String[] id, Integer state) {
        Query query = new Query(Criteria.where("id").is(id));
        List<Publish> publishList = mongoTemplate.find(query, Publish.class);
        for (Publish publish : publishList) {
            if (publish.getState()==0 || publish.getState()==2){
                Update update = new Update();
                update.set("state",state);
                mongoTemplate.updateFirst(query,update,Publish.class);
            }
        }
        return true;
    }
}
